#include <engine.h>
#include <QList>
#include <QCoreApplication>

GObject::GObject(QString& name, QGraphicsItem *parent)
    : QGraphicsObject(parent)
{
	m_name = name;
    m_bGarbage = false;
}

GObject::~GObject()
{
}

bool GObject::GetGarbage()
{
	return m_bGarbage;
}

void GObject::Destroy()
{
	m_bGarbage = true;
	QCoreApplication::instance()->postEvent(GEngine::GetGame(), new GEvent(GEvent::Evt_Garbage));
}

const QString& GObject::GetName()
{
    return m_name;
}

bool GObject::event(QEvent* event)
{
	GEvent* pEvent = static_cast<GEvent*>(event);
	if( pEvent->type() == GEvent::Evt_MoveTo || pEvent->type() == GEvent::Evt_RotateTo || 
		pEvent->type() == GEvent::Evt_ScaleTo || pEvent->type() == GEvent::Evt_AlphaTo ||
		pEvent->type() == GEvent::Evt_Effect )
	{
		FinishedAnimation(pEvent->type(), pEvent->param());
	}
	else if(pEvent->type() == GEvent::Evt_Collision) 
	{
		CollisionDetected(pEvent->param());
	}
	
    return QGraphicsObject::event(event);
}

void GObject::FinishedAnimation(QEvent::Type type, void* param)
{
}

void GObject::CollisionDetected(void* param)
{
}

void GObject::StartAnimation(QEvent::Type type, QVariant value, double duration, QGraphicsItem* pItem)
{
	if(duration == 0)
		return;

	if(pItem == 0)
		pItem = this;

	GAnimate *pAni = new GAnimate(type, pItem, value, duration);
    GEngine::GetGame()->AddAnimate(pAni);
}

void GObject::StopAnimation()
{
    GAnimates* pAniList = GEngine::GetGame()->GetAnimateObjects();
    if(!pAniList || pAniList->size() <= 0)
        return;

    GAnimates finish;
    GAnimates_itor it = pAniList->begin();
    GAnimates_itor end = pAniList->end();
    while(it != end)
    {
        GAnimate *pAni = *it;
        Q_ASSERT(pAni);

		if(pAni->GetItem() == this)
		{
			it = pAniList->erase(it);
			finish.push_back(pAni);
			qDebug() << "stop";
			continue;
		}
		
		++it;
    }

    GAnimates_itor itor;
    for(itor = finish.begin(); itor != finish.end(); itor++)
    {
        GAnimate* pAni = *itor;
        delete pAni;
    }
}

//void GObject::MoveTo(QPoint& to, double duration)
//{
//    m_stopAnimate = false;
//    GAnimate *pAni = new GAnimate("move", this, to, duration);
//    GEngine::GetGame()->AddAnimateObject(pAni);
//}
//
//void GObject::RotateTo(double angle, double duration)
//{
//    m_stopAnimate = false;
//    GAnimate *pAni = new GAnimate("rotate", this, angle, duration);
//    GEngine::GetGame()->AddAnimateObject(pAni);
//}

//void GObject::ScaleTo()
//{
//    m_stopAnimate = false;
//}
//
//void GObject::AlphaTo()
//{
//    m_stopAnimate = false;
//}
/*
double GObject::GetAngle(double toX, double toY, QGraphicsItem* pItem)
{
	//QGraphicsItem *parent = (pItem == 0) ? this : pItem->parentItem();
	QPointF pt;
	if(pItem == 0)
	{
		pItem = this;
	}
	else
	{
		QPointF pos = QPointF(pItem->x(), pItem->y());
		pItem->mapToParent(pos);
		pt = pos - QPointF(toX, toY);
	}


	double anglePI = degree_to_radian(pItem->rotation());
    QVector3D vtFrom(qSin(anglePI), qCos(anglePI), 0);

	qDebug() << pItem->x() << pItem->y();
    //QVector3D vtTo(pItem->x() - toX, pItem->y() - toY, 0);
	QVector3D vtTo(pt.x(), pt.y(), 0);
    //for window position
    vtTo.setX(-vtTo.x());

    vtFrom.normalize();
    vtTo.normalize();
    qDebug() << "vtFrom" << vtFrom << "vtTo" << vtTo;

    double dot = QVector3D::dotProduct(vtFrom, vtTo);
//    if(dot == 0)
//        qDebug() << "degree 90";
//    else if(dot > 0)
//        qDebug() << "degree less than 90";
//    if(dot < 0)
//        qDebug() << "degree more than 90";

    anglePI = qAcos(dot);
//    if(anglePI == 0)
//    {
//        qDebug() << "0 degree";
//        return;
//    }
    double angle = radian_to_degree(anglePI);
    if(QVector3D::crossProduct(vtFrom, vtTo).z() >= 0)
        angle *= -1;

    return angle;

    //degreeTo = 90;
    //double duration = 1000*angle/ROTATION_BODY_VELOCITY;
    //qDebug("!!!!!!!!!!!rotate %f -> %f for %f time!!!", rotation(), angle, duration);
}

//bool GObject::CreateObject(QString img_path)
//{
//    if(!m_image.Load(img_path))
//        return false;

//    GWindow* pWindow = GEngine::GetEngine()->GetWindow();
//    pWindow->addItem(this);
//}
*/
